<?php
namespace Admin\Controller;
use Think\PageAdmin;
use Common\Support\Curl;
use Think\Upload;
use Admin\Controller\QiniuController;

/**
 * 视频管理控制器
 */
class VideoController extends BaseController
{

    /**
     * 初始化
     * 
     * @return [type] [description]
     */
    public function _initialize()
    {
        parent::_initialize();       
    }

    public function index(){
        $prefix = C('DB_PREFIX');

        $count = M('video')->count();
        $page  = new PageAdmin($count, C('PAGE_NUM'));
        
        $pages['show']  = $page->show();  //分页输出
        $pages['total'] = $count;  //总数
        $pages['lists'] = M('video a')
            ->order("a.id desc")
            ->limit($page->firstRow.','.$page->listRows)
            ->select();

        //$this->seotitle = '视频列表';
        $this->pages    = $pages;
        $this->display();
    }

    public function add(){
        $Qiniu = new QiniuController();
        $this->token = $Qiniu->getToken();
        S('token',$this->token);
        $this->categorys = M('teacher')->select();
        $this->seotitle="添加视频";
        $this->display();
    }

    /**
     * 文章保存
     * @return [type] [description]
     */
    public function save()
    {
        $action = I('get.action', '', 'trim');
        $params = I('post.');

        if($params['title'] == ''){
            $this->error('请输入标题');
        }

        if($params['description'] == ''){
            $this->error('请输入描述');
        }

        if($params['video_url'] == ''){
            $this->error('请上传视频');
        }

        if($params['teacher_id'] == '0'){
            $this->error('请选择作者');
        }


        // 新增
        if ($action == 'add') {

            M('video')->add([
                'title'       => trim($params['title']),
                'description' => $params['description'],
                'status'      => $params['status'] == 1 ? 1 : 0,
                'sortid'      => intval($params['sortid']),
                'video_url'   => $params['video_url'],
                'remark'      => $params['remark'],
                'teacher_id'  => $params['teacher_id'],
                'addtime'     => time(),
                'updatetime'  => time()
            ]);

            $this->success('操作成功', U('index'));
        }

        // 编辑保存
        elseif ($action == 'edit') {
            $id = I('post.id', 0, 'intval');            


            M('video')->where(['id' => $id])->save([
                'title'       => trim($params['title']),
                'description' => $params['description'],
                'status'      => $params['status'] == 1 ? 1 : 0,
                'sortid'      => intval($params['sortid']),
                'teacher_id'  => $params['teacher_id'],
                'remark'      => $params['remark'],
                'video_url'   => $params['video_url'],
                'updatetime'  => time()
            ]);

            $this->success('操作成功', U('index'));
        }
    }

    public function uptoken(){
        return S('token');
    }

    

}